package com.edu.admin.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import com.edu.admin.common.BaseService;
import com.edu.admin.entity.SysSchool;
import com.edu.admin.entity.SysSchoolPhase;
import com.edu.admin.enums.ExceptionEnum;
import com.edu.admin.exception.SysException;
import com.edu.admin.mapper.SchoolMapper;
import com.edu.admin.vo.PhaseVo;
import com.edu.admin.vo.SchoolVo;
import com.edu.admin.vo.SchoolPhaseVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * 学校服务
 * 
 * @author Wangjiong
 * 
 */
@Transactional
@Service
public class SchoolService extends BaseService<SchoolMapper, SysSchool> {
	
	@Autowired
	private SchoolPhaseService schoolPhaseService;
	
	/**
	 * 获取学校信息
	 * 
	 * @param schoolId
	 * 
	 * @return SchoolVo
	 */
	public SchoolVo getSchool(Long schoolId){
		
		SchoolVo school = this.mapper.getSchool(schoolId);
		
		List<SchoolPhaseVo> schoolPhaseList = schoolPhaseService.getSchoolPhaseListBySchoolId(school.getSchoolId());
        school.setSchoolPhaseList(schoolPhaseList);

		return school;
	}
	
	/**
	 * 条件搜索学校列表 （分页）
	 * 
	 * @param pageNum
	 * @param pageSize
	 * @param provinceCode
	 * @param cityCode
	 * @param districtCode
	 * @param schoolName
	 * 
	 * @return PageInfo<SchoolVo>
	 */
	public PageInfo<SchoolVo> getSchoolListPage(Integer pageNum, Integer pageSize, 
			String provinceCode, String cityCode,String districtCode,String schoolName) {
		if (pageNum == null) {
			pageNum = 1;
		}
		if (pageSize == null) {
			pageSize = 10;
		}
		PageHelper.startPage(pageNum, pageSize);
		
		List<SchoolVo> schoolList = this.mapper.getSchoolList(provinceCode, cityCode, districtCode, schoolName);
		
		for (SchoolVo school:schoolList)
		{
            List<SchoolPhaseVo> schoolPhaseList = schoolPhaseService.getSchoolPhaseListBySchoolId(school.getSchoolId());
            school.setSchoolPhaseList(schoolPhaseList);
        }
		
		PageInfo<SchoolVo> pageInfo = new PageInfo<SchoolVo>(schoolList);

		return pageInfo;
	}
	
	/**
	 * 获取可用学校列表
	 * 
	 * @param provinceCode
	 * @param cityCode
	 * @param districtCode
	 * @param schoolName
	 * 
	 * @return List<SchoolVo>
	 */
	public List<SchoolVo> getSchoolList(String provinceCode, String cityCode,
											String districtCode,String schoolName) {
	
		List<SchoolVo> schoolList = this.mapper.getSchoolList(provinceCode, cityCode, districtCode, schoolName);
		
		for (SchoolVo school:schoolList)
		{
            List<SchoolPhaseVo> schoolPhaseList = schoolPhaseService.getSchoolPhaseListBySchoolId(school.getSchoolId());
            List<Long> phaseList = new ArrayList<Long>();

            if (schoolPhaseList!=null && schoolPhaseList.size()>0)
            {
	            for (SchoolPhaseVo schoolPhaseVo: schoolPhaseList)
	            {
	            	phaseList.add(schoolPhaseVo.getPhaseId());
	            }
	            
            }
            
            int size = phaseList.size();  
            Long[] phases = (Long[])phaseList.toArray(new Long[size]);  
            
            school.setPhases(phases);
            school.setSchoolPhaseList(schoolPhaseList);
        }

		return schoolList;
	}
	
	/**
	 * 获取所有学校列表（包括停用学校）（分页）
	 * 
	 * @param pageNum
	 * @param pageSize
	 * @param provinceCode
	 * @param cityCode
	 * @param districtCode
	 * @param schoolName
	 * 
	 * @return PageInfo<SchoolVo>
	 */
	public PageInfo<SchoolVo> getAllSchoolListPage(Integer pageNum, Integer pageSize, 
			String provinceCode, String cityCode,String districtCode,String schoolName){
		if (pageNum == null) {
			pageNum = 1;
		}
		if (pageSize == null) {
			pageSize = 10;
		}
		PageHelper.startPage(pageNum, pageSize);
		
		List<SchoolVo> schoolList = this.mapper.getAllSchoolList(provinceCode, cityCode, districtCode, schoolName);
		
		for (SchoolVo school:schoolList)
		{
            List<SchoolPhaseVo> schoolPhaseList = schoolPhaseService.getSchoolPhaseListBySchoolId(school.getSchoolId());
            List<Long> phaseList = new ArrayList<Long>();

            if (schoolPhaseList!=null && schoolPhaseList.size()>0)
            {
	            for (SchoolPhaseVo schoolPhaseVo: schoolPhaseList)
	            {
	            	phaseList.add(schoolPhaseVo.getPhaseId());
	            }
	            
            }
            
            int size = phaseList.size();  
            Long[] phases = (Long[])phaseList.toArray(new Long[size]);  
            
            school.setPhases(phases);
            school.setSchoolPhaseList(schoolPhaseList);
        }
		
		PageInfo<SchoolVo> pageInfo = new PageInfo<SchoolVo>(schoolList);

		return pageInfo;
	}
	
	
	/**
	 * 获取所有学校列表（包括停用学校）
	 * 
	 * @param provinceCode
	 * @param cityCode
	 * @param districtCode
	 * @param schoolName
	 * 
	 * @return List<SchoolVo>
	 */
	public List<SchoolVo> getAllSchoolList(String provinceCode, String cityCode,String districtCode,String schoolName){
		
		List<SchoolVo> schoolList = this.mapper.getAllSchoolList(provinceCode, cityCode, districtCode, schoolName);
		
		for (SchoolVo school:schoolList)
		{
            List<SchoolPhaseVo> schoolPhaseList = schoolPhaseService.getSchoolPhaseListBySchoolId(school.getSchoolId());
            List<Long> phaseList = new ArrayList<Long>();

            if (schoolPhaseList!=null && schoolPhaseList.size()>0)
            {
	            for (SchoolPhaseVo schoolPhaseVo: schoolPhaseList)
	            {
	            	phaseList.add(schoolPhaseVo.getPhaseId());
	            }
	            
            }
            
            int size = phaseList.size();  
            Long[] phases = (Long[])phaseList.toArray(new Long[size]);  
            
            school.setPhases(phases);
            school.setSchoolPhaseList(schoolPhaseList);
        }

		return schoolList;
	}
	
	/**
     * 学校重复性检查
     * 
     * @param SysSchool school
     * 
     * @return List<SchoolVo>
     * @throws Exception
     */
    public List<SchoolVo> checkSchool(SysSchool school) throws Exception {
    	
    	return this.mapper.findSchoolBySchoolName(school.getSchoolId(), 
    											school.getSchoolName().trim(),
    											school.getProvinceCode(),
    											school.getCityCode(),
    											school.getDistrictCode());
  
    }
	
	/**
	 * 学校保存
	 * 
	 * @param SysSchool school
	 * 
	 * @return Map<String, Object>
	 * 
	 * @throws Exception 
	 */
	public Map<String, Object> saveSchool(SysSchool school) throws Exception {
		Map<String, Object> result = new HashMap<>();
		
		List<SchoolVo> schoolList = checkSchool(school);
		
		if (schoolList!=null && schoolList.size()>0)
		{
			throw new SysException(ExceptionEnum.SCHOOLNAMEREPEAT);
		}
		
		Long schoolId = school.getSchoolId();

		if (schoolId == null) {
			
			insert(school);
			
			schoolId = school.getSchoolId();


		} else {
			
			updateByPrimaryKeySelective(school);

		}
		
		schoolPhaseService.deleteSchoolPhaseBySchoolId(schoolId);
		
		Long[] phases = school.getPhases();
		
		if (phases!=null && phases.length >0)
		{
			for (int i=0;i<phases.length;i++)
			{
				SysSchoolPhase schoolPhase = new SysSchoolPhase();
				schoolPhase.setSchoolId(schoolId);
				schoolPhase.setPhaseId(new Long(phases[i]));
				schoolPhaseService.insert(schoolPhase);
			}
		}
				
		result.put("schoolId", schoolId);

		return result;

	}
	
	/**
	 * 删除学校
	 * 
	 * @param schoolId
	 * 
	 * @return int
	 */
	public int deleteSchool(Long schoolId) {
		
		if (schoolId != null) {
			return this.deleteByPrimaryKey(schoolId);

		}
		
		return 0;	

	}
	
	/**
	 * 启用停用学校
	 * 
	 * @param schoolId
	 * @param useType
	 * 
	 * @return int
	 */
	public int useSchool(Long schoolId,Integer useType) {
		
		if (schoolId != null) {
			SysSchool school = new SysSchool();
			school.setSchoolId(schoolId);
	        school.setUseType(useType);
			return updateByPrimaryKeySelective(school);

		}
		
		return 0;	

	}
	
	/**
	 * 统计更新学校班级数
	 * 
	 * @param schoolId
	 * 
	 * @return int
	 */
	public int updateSchoolClassesNum(Long schoolId) {
		
		return this.mapper.updateSchoolClassesNum(schoolId);

	}
	
	/**
	 * 统计更新学校教师数
	 * 
	 * @param schoolId
	 * 
	 * @return int
	 */
	public int updateSchoolTeacherNumBySchoolId(Long schoolId) {
		
		return this.mapper.updateSchoolTeacherNumBySchoolId(schoolId);

	}
	
	/**
	 * 统计更新学校学生数
	 * 
	 * @param schoolId
	 * 
	 * @return int
	 */
	public int updateSchoolStudentNumBySchoolId(Long schoolId) {
		
		return this.mapper.updateSchoolStudentNumBySchoolId(schoolId);

	}
	
	/**
	 * 统计更新所有学校教师数
	 * 
	 * @param
	 * 
	 * @return int
	 */
	public int updateSchoolTeacherNum() {
		
		return this.mapper.updateSchoolTeacherNum();

	}
	
	/**
	 * 统计更新所有学校学生数
	 * 
	 * @param
	 * 
	 * @return int
	 */
	public int updateSchoolStudentNum() {
		
		return this.mapper.updateSchoolStudentNum();

	}
	
	/**
	 * 获取学校的学段列表
	 * 
	 * @param schoolId
	 * 
	 * @return List<PhaseVo>
	 */
	public List<PhaseVo> getSchoolPhaseList(Long schoolId) {

		return this.mapper.getSchoolPhaseList(schoolId);
	
	}
}
